/*! *********************************************************************************
* \addtogroup Private Profile Service
* @{
********************************************************************************** */
/*! *********************************************************************************
* Copyright (c) 2014, Freescale Semiconductor, Inc.
* * Copyright 2022 NXP
* All rights reserved.
*
* \file
*
* SPDX-License-Identifier: BSD-3-Clause
********************************************************************************** */

/************************************************************************************
*************************************************************************************
* Include
*************************************************************************************
************************************************************************************/
#include "ble_general.h"
#include "gatt_db_app_interface.h"
#include "gatt_server_interface.h"
#include "gap_interface.h"
#include "ble_profile_interface.h"
#include "gatt_db_handles.h"
/************************************************************************************
*************************************************************************************
* Private constants & macros
*************************************************************************************
************************************************************************************/

/************************************************************************************
*************************************************************************************
* Private type definitions
*************************************************************************************
************************************************************************************/
#if defined(gGattDbDynamic_d) && (gGattDbDynamic_d > 0)
const uint8_t uuid_service_qpps[16] = 				{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E};
const uint8_t uuid_qpps_characteristics_tx[16] = 	{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E};
const uint8_t uuid_qpps_characteristics_rx[16] = 	{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E};

const uint8_t uuid_service_nearby[16] = 			{0xba, 0x2d, 0x9c, 0x68, 0x73, 0x30, 0x33, 0x86, 0xb2, 0x4b, 0x17, 0x08, 0x40, 0x3e, 0xfe, 0x48};
const uint8_t uuid_nearby_AccessoryData[16] = 		{0x28, 0x33, 0xf5, 0x75, 0x73, 0x80, 0x4e, 0x9a, 0x21, 0x47, 0xef, 0xd8, 0xd5, 0xd9, 0xe8, 0x95};
#endif

/************************************************************************************
*************************************************************************************
* Private memory declarations
*************************************************************************************
************************************************************************************/

/*! Battery Service - Subscribed Client*/
deviceId_t mQpp_SubscribedClientId;

/************************************************************************************
*************************************************************************************
* Public functions
*************************************************************************************
************************************************************************************/

bleResult_t Qpp_Start (qppsConfig_t *pServiceConfig)
{
    return gBleSuccess_c;
}

bleResult_t Qpp_Stop (qppsConfig_t *pServiceConfig)
{
    mQpp_SubscribedClientId = gInvalidDeviceId_c;
    return gBleSuccess_c;
}

bleResult_t Qpp_Subscribe(deviceId_t clientDeviceId)
{
    mQpp_SubscribedClientId = clientDeviceId;
    return gBleSuccess_c;
}

bleResult_t Qpp_Unsubscribe()
{
    mQpp_SubscribedClientId = gInvalidDeviceId_c;
    return gBleSuccess_c;
}

bleResult_t Qpp_SendData (uint8_t deviceId, uint16_t serviceHandle,uint16_t length, uint8_t *testData)
{
    uint16_t  handle;
    bleResult_t result;
    uint16_t  handleCccd;
    bool_t isNotifActive;

    bleUuid_t uuid;
    FLib_MemCpy(uuid.uuid128, uuid_qpps_characteristics_tx, 16);

    /* Get handle of  characteristic */
    result = GattDb_FindCharValueHandleInService(serviceHandle, gBleUuidType128_c, &uuid, &handle);
    if (result != gBleSuccess_c)
        return result;
    /* Get handle of CCCD */
    if ((result = GattDb_FindCccdHandleForCharValueHandle(handle, &handleCccd)) != gBleSuccess_c)
        return result;

    result = Gap_CheckNotificationStatus(deviceId, handleCccd, &isNotifActive);
    if ((gBleSuccess_c == result) && (TRUE == isNotifActive))
        result = GattServer_SendInstantValueNotification(deviceId, handle, length, testData);

    return result;
}


bleResult_t Update_Nearby(uint8_t deviceId, uint16_t size, uint8_t *testData)
{
    uint16_t handle;
    bleResult_t result;
    bleUuid_t uuid;

    FLib_MemCpy(uuid.uuid128, uuid_nearby_AccessoryData, 16);

    result = GattDb_FindCharValueHandleInService(service_nearby, gBleUuidType128_c, &uuid, &handle);
    if (result != gBleSuccess_c)
        return result;

    result = GattDb_WriteAttribute(handle, size, testData);

    return result;
}

bleResult_t Erase_Nearby(uint8_t deviceId)
{
    uint16_t handle;
    bleResult_t result;
    bleUuid_t uuid;
    uint8_t blank_data[48] = {0x00};

    FLib_MemCpy(uuid.uuid128, uuid_nearby_AccessoryData, 16);

    result = GattDb_FindCharValueHandleInService(service_nearby, gBleUuidType128_c, &uuid, &handle);
    if (result != gBleSuccess_c)
        return result;

    result = GattDb_WriteAttribute(handle, sizeof(blank_data), blank_data);

    return result;
}
